using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ShowInfo : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI userInfo;
    [SerializeField] private GameObject deathMenuHeader;
    [SerializeField] private GameObject nextLvlButton;

    private void Start() { // Отображение информации об игроке в конце игры
        deathMenuHeader.SetActive(PlayerPrefs.GetString("endGame") == "win");
        nextLvlButton.SetActive(PlayerPrefs.GetString("endGame") == "win");

        if (PlayerPrefs.GetInt("playersCount") == 1) {
            userInfo.text = "Player " + PlayerPrefs.GetString("nickname") + " has got " + PlayerPrefs.GetInt("result") + " points!";
        } else {
            userInfo.text = "Players " + PlayerPrefs.GetString("nickname") + " and " + PlayerPrefs.GetString("second_nickname") + 
                " have got " + PlayerPrefs.GetInt("result") + " points!";
        }

        string endGame_temp = userInfo.text;
        // if (PlayerPrefs.GetString("endGame") == "win") userInfo.text = endGame_temp + " [survived]";
        // else userInfo.text = endGame_temp + " [dead]";
    }

    public void GenerateNextLevel() {
        PlayerPrefs.SetString("continue", "yes");
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
    }

    public void Restart() {
        PlayerPrefs.SetString("continue", "no");
        PlayerPrefs.SetInt("result", 0);
        string temp = PlayerPrefs.HasKey("stat") ? PlayerPrefs.GetString("stat") : "";
        PlayerPrefs.SetString("stat", temp + "\n" + userInfo.text); 
        PlayerPrefs.SetString("continue", "no");
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
    }

    public void BackToMenu() {
        string temp = PlayerPrefs.HasKey("stat") ? PlayerPrefs.GetString("stat") : "";
        PlayerPrefs.SetString("stat", temp + "\n" + userInfo.text); 
        PlayerPrefs.SetString("continue", "no");
        PlayerPrefs.SetInt("result", 0);
        SceneManager.LoadScene(0);
    }
}
